package org.oss.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;

import org.apache.log4j.Logger;

import com.oss.base.BaseConst;
import com.oss.vo.OssStatistics;

public class LogReader {
	static Logger logger = Logger.getLogger(LogReader.class);

	public static void main(String[] args) throws IOException {
		logger.info("begin");
		File file=new File("E:/工作/项目/云存储OSS/test/20160121/1");
		logger.info("file size:"+file.length());
        String key = "1";
		InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");//考虑到编码格式
        //public%2F20160121%2F" - 8 "-" 406 "1501753186949068" 0 "-
        BufferedReader bufferedReader = new BufferedReader(read);
        String tmp ="";
        int i =0;
        HashMap<String, OssStatistics> map = new HashMap<String, OssStatistics>();
		
		while((tmp = bufferedReader.readLine()) != null){
//			Scanner sc = new Scanner(System.in);
			i++;
//			System.out.println("第"+(++i)+"行");

			try {
				OssStatistics ossStat = buildStatistics(tmp.split("\" \""),key);
				
				if(map.containsKey(ossStat.genKey())){
					map.put(ossStat.genKey(), updateStat(map.get(ossStat.genKey()),ossStat));
				}else{
					map.put(ossStat.genKey(),ossStat);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		logger.info("end"+i);
	}
	
	private static OssStatistics updateStat(OssStatistics oss,
			OssStatistics ossStat) {
		oss.setSucVisits(oss.getSucVisits()+ossStat.getSucVisits());
		oss.setFaledVisits(oss.getFaledVisits()+ossStat.getFaledVisits());
		oss.setTotalDownFlow(oss.getTotalDownFlow()+ossStat.getTotalDownFlow());
		oss.setTotalUpFlow(oss.getTotalUpFlow()+ossStat.getTotalUpFlow());
		oss.setTotalOssCost(oss.getTotalOssCost()+ossStat.getTotalOssCost());
		oss.setTotalReqCost(oss.getTotalReqCost()+ossStat.getTotalReqCost());
		oss.setTotalVisits(oss.getTotalVisits()+ossStat.getTotalVisits());
		
		oss.setAvgUpFlow(oss.getTotalUpFlow()/oss.getTotalVisits());
		oss.setAvgDownFlow(oss.getTotalDownFlow()/oss.getTotalVisits());
		oss.setAvgOssCost(oss.getTotalOssCost()/oss.getTotalVisits());
		oss.setAvgReqCost(oss.getTotalReqCost()/oss.getTotalVisits());
		return oss;
	}

	private static OssStatistics buildStatistics(String[] tmps, String key) throws Exception {
		OssStatistics oss = new OssStatistics();
		String[] infos = tmps[0].split(" ");
		String[] objInfos = tmps[8].split(" ");
		
		if(BaseConst.HTTP_STATUS_SUCCESS.equalsIgnoreCase(infos[9])){
			oss.setSucVisits(1);
			oss.setFaledVisits(0);
		}else{
			oss.setSucVisits(0);
			oss.setFaledVisits(1);
		}
		
		if(infos[6].endsWith(BaseConst.HTTP_REQUEST_GET)){
			oss.setAvgDownFlow(parse(infos[10]));
			oss.setTotalDownFlow(parse(infos[10]));
			oss.setAvgUpFlow(0);
			oss.setTotalUpFlow(0);
		}else{
			long size = parse(objInfos[1]);
			long changSize = parse(objInfos[6]);
			oss.setAvgDownFlow(0);
			oss.setTotalDownFlow(0);
			oss.setAvgUpFlow(size == 0?changSize:size);
			oss.setTotalUpFlow(size == 0?changSize:size);
		}
		
		oss.setAvgOssCost(parse(objInfos[3]));
		oss.setAvgReqCost(parse(infos[11]));
		oss.setTotalOssCost(parse(objInfos[3]));
		oss.setTotalReqCost(parse(infos[11]));
		oss.setFileName(key);
		oss.setMethod(tmps[6]);
		oss.setOwnerId(objInfos[5]);
		oss.setOssKey(objInfos[0].split("%2F")[0]);
		oss.setBucket(tmps[7]);
		oss.setEndpoint(tmps[2]);
		oss.setReqId(tmps[5]);
		oss.setTotalVisits(1);
		
		return oss;
	}
	
	public static long parse(String s) throws Exception{
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
			return 0;
		}
		
		
	}
	
}
